namespace CIS
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class UserCustomerSetPage : Page
    {
        protected DropDownList AreaDdl;
        protected DropDownList catDdl;
        protected DataGrid customerListDg;
        protected HyperLink Hk;
        protected Image iCustomerInfo;
        protected HyperLink interfaceHk;
        protected Label lbArea;
        protected Label lbCat;
        protected Label lbProvince;
        protected UserPrincipal Principal = null;
        protected DropDownList provinceDdl;
        protected Button SearchBtn;

        private void AreaDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.provinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.AreaDdl.SelectedValue));
            this.provinceDdl.DataTextField = "RegionName";
            this.provinceDdl.DataValueField = "RegionId";
            this.provinceDdl.DataBind();
            this.catDdl.DataSource = ManageCAT.GetCAT(new Guid(this.AreaDdl.SelectedValue));
            this.catDdl.DataTextField = "CATGroupName";
            this.catDdl.DataValueField = "CATGroupId";
            this.catDdl.DataBind();
        }

        private void customerListDg_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string str;
            string cmd = e.CommandName;
            string strCustomerId = e.CommandArgument.ToString();
            if (((str = cmd) != null) && (string.IsInterned(str) == "Edit"))
            {
                base.Response.Redirect("UserCustomerSetEditPage.aspx?CustomerId=" + strCustomerId, true);
            }
        }

        private void customerListDg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.customerListDg.CurrentPageIndex = e.NewPageIndex;
            this.CustomerListDgDataBind();
        }

        private void CustomerListDgDataBind()
        {
            this.customerListDg.DataSource = ManageCustomer.GetCustomerBasicInfoForTranEdit(new Guid(this.AreaDdl.SelectedValue), new Guid(this.provinceDdl.SelectedValue), new Guid(this.catDdl.SelectedValue));
            this.customerListDg.DataBind();
        }

        private void FillPage()
        {
            this.CustomerListDgDataBind();
        }

        private void InitializeComponent()
        {
            this.AreaDdl.SelectedIndexChanged += new EventHandler(this.AreaDdl_SelectedIndexChanged);
            this.SearchBtn.Click += new EventHandler(this.SearchBtn_Click);
            this.customerListDg.ItemCommand += new DataGridCommandEventHandler(this.customerListDg_ItemCommand);
            this.customerListDg.PageIndexChanged += new DataGridPageChangedEventHandler(this.customerListDg_PageIndexChanged);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFrom();
            this.AreaDdl.Items.Clear();
            Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
            for (int i = 0; i < area.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Text = area[i].AreaName;
                tmpListItem.Value = area[i].AreaId.ToString();
                this.AreaDdl.Items.Add(tmpListItem);
            }
            this.provinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.AreaDdl.SelectedValue));
            this.provinceDdl.DataTextField = "RegionName";
            this.provinceDdl.DataValueField = "RegionId";
            this.provinceDdl.DataBind();
            this.catDdl.DataSource = ManageCAT.GetCAT(new Guid(this.AreaDdl.SelectedValue));
            this.catDdl.DataTextField = "CATGroupName";
            this.catDdl.DataValueField = "CATGroupId";
            this.catDdl.DataBind();
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.InitPage();
                this.SetFromImage();
                this.FillPage();
            }
        }

        private void SearchBtn_Click(object sender, EventArgs e)
        {
            this.customerListDg.DataSource = ManageCustomer.GetCustomerBasicInfoForTranEdit(new Guid(this.AreaDdl.SelectedValue), new Guid(this.provinceDdl.SelectedValue), new Guid(this.catDdl.SelectedValue));
            this.customerListDg.DataBind();
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbArea.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetPage", "lbArea", language, path);
            this.lbCat.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetPage", "lbCat", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbProvince.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetPage", "lbProvinceOverSea", language, path);
            }
            else
            {
                this.lbProvince.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetPage", "lbProvince", language, path);
            }
            this.SearchBtn.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetPage", "SearchBtn", language, path);
            this.customerListDg.Columns[0].HeaderText = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetPage", "Name", language, path);
            this.customerListDg.Columns[1].HeaderText = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetPage", "Title", language, path);
            this.customerListDg.Columns[2].HeaderText = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetPage", "Company", language, path);
            this.customerListDg.Columns[3].HeaderText = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetPage", "Gender", language, path);
            this.customerListDg.Columns[4].HeaderText = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetPage", "Depart", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iCustomerInfo.ImageUrl = "images/title_CustomerOwershipSetting.jpg";
                    break;

                case "en":
                    this.iCustomerInfo.ImageUrl = "images/title_CustomerOwershipSettingEn.jpg";
                    break;
            }
        }
    }
}

